/**
 * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
 * @file      MotorControlFunction.c
 * @author    Fortiortech  Appliction Team
 * @since     Create:2022-08-05
 * @date      Last modify:2022-08-05
 * @note      Last modify author is Leo.li
 * @brief
 */
#include <MyProject.h>


/** 
 * @brief        P11外部中断睡眠引脚设置
 * @date         2022-09-13
 */
void SleepMode_Init(void)
{
    ClrBit(P1_OE, P11);                     // config P11 as input
    ClrBit(P1_IF, P11);                     // clear P11 interrupt flag
    ClrBit(P1_IE, P11);                     // config P11 as the source of EXTI1
    IT11 = 1;
    IT10 = 0;                               // 00: posedge mode interrupt，01: negedge mode interrupt，1x: edge-change mode interrupt
    SetBit(IP0, PX11);
    SetBit(IP0, PX10);                      // 中断优先级别0，中断优先级最高
    EX1 = 1;                                // 使能外部中断1, P11位外部中断输入硬件
    EA = 1;                                 // 使能全局中断
}
/*! @brief put controller to sleep mode：将控制器调到睡眠模式
 * @details
 * afterwards, only a wake-up event (S, LIN, PWM) can bring the controller back to life 之后，只有一个唤醒事件(S, LIN, PWM)可以把控制器带回生活;)
 * current consumption typ. <100uA电流消耗 <100uA
 *
 * nice traps: pull-up of pin has to be disabled before going to sleep, otherwise the falling
 * edge at pin causes instant wake-up; also, IC must not go to sleep when gate driver short-circuit
 * error condition is active - in sleep mode, gate driver is connected to GND and could thereby
 * short-circuit VBAT and GND
 *良好的陷阱:睡眠前必须禁用引脚的上拉，否则引脚的下降沿会导致立即唤醒;同时，
  当门驱动器短路错误状态处于活动状态时，IC不能进入睡眠状态——在睡眠模式下，
  门驱动器连接到GND，从而导致VBAT和GND短路
 * @param         - (none)
 * @return        - (none)
 *
 */
void sc_goToSleep(void) {

    if((mcPwmInput.Duty == 0)||(mcPwmInput.Duty == 32768))//PWM信号输入悬空时进入休眠，悬空时PWM上拉到12V
    {
        SleepSet.SleepDelayCout++;
        if ((SleepSet.SleepDelayCout >= 10000)&&(SleepSet.SleepFlag ==0)) //最大65530，若要再大，需改数据类型，10S进入休眠
        {
            MOE     = 0;
                                    
            /******睡眠模式初始化**********/
            SleepMode_Init();
            
            ClrBit(DRV_CR, FOCEN);  //关闭FOC
            ClrBit(ADC_CR, ADCEN);
            ClrBit(CCFG1, WDT_EN);  //关闭WDT
            //关闭运放
            ClrBit(AMP_CR0, AMP0EN);         //AMP0 Enable
            ClrBit(AMP_CR0, AMP1EN);         //AMP1 Enable
            ClrBit(AMP_CR0, AMP2EN);         //AMP2 Enable
            //关闭比较器
            ClrBit(CMP_CR1, CMP3EN);    //CMP3 Enable
            //关闭VREF
            SetBit(VREF_VHALF_CR, 0x00);

            
//            /*- WLK175-PCB02-1产品板，没有引出的IO配置上拉，引出IO每个都需要有固定状态—-*/
//            P0_PU = P00 | P01 | P02 | P03 | P04 | P05 | P06| P07; // 需确认这些端口能接受上拉
//            
//            /*-  P10和P11为开漏硬件上需要根据实际加上拉,P11配置为外部中断引脚，不需要再配置 -*/
//            P1_OE =  P10 | P12 | P13 | P14 | P15 | P15 | P17;
//            GP10 =0; GP12 =0; GP13 = 0; GP14 =0;GP15 = 0; GP16 =0;GP17 =0;
//            
//            P2_PU = P20 | P21| P24;
//            P2_OE = P22 | P23 | P25| P26| P27;
//            GP22 = 0; GP23 = 0; GP25 = 0; GP26 = 0; GP27 = 0; 
//            
//            P3_PU = P34 | P35| P36| P37;                         // 需确认这些端口能接受上拉
//            P3_OE = P30 | P31| P32| P33;
//            GP30 = 0; GP31 = 0;GP32 = 0;GP33 = 0;
//            
//            P4_PU = P44 | P45|P46;                                // 需确认这些端口能接受上拉
//            P4_OE = P43;  
//            GP43 = 0;
                              
            #if (1)
            /*- FU6866Q1_V2.0Demo板配置：悬空引脚配置为上拉，反电动势配置为输出0，输入检测引脚配置为输出0—-*/
            /*- 没有引出的IO配置上拉，引出IO每个都需要有固定状态—-*/
            P2_OE = P20| P21 | P22 | P23 | P24 | P25 | P26 | P27;
            GP21 = 0;GP21 = 0; GP22 = 0; GP23 = 0; GP24 = 0;GP25 = 0; GP26 = 0;  GP27 = 0;
            
            P3_OE = P30 | P31 | P32 | P34 | P35;
            P3_PU = P33 | P34;            // 需确认这些端口能接受上拉
            GP30 = 0; GP31 = 0; GP32 = 0; GP34 = 0; GP35 = 0;
            
            P4_OE = P43 | P44 | P46; 
            P4_PU = P41 | P42 | P45;  
            GP43 =0; GP44 =0; GP46 =0; 
            
            
            P0_OE =  P01 | P02 | P03 | P04 | P05 | P06 | P07;
            P0_PU = P00 ; // 需确认这些端口能接受上拉
            GP01 = 0; GP02 = 1; GP03 = 1; GP04 = 1;   GP05 = 1 ;GP06 = 1;GP07 = 1;
            
            /*-  P10和P11为开漏硬件上需要根据实际加上拉 -*/
            P1_OE = P10  | P12 | P13 | P14 | P15 | P16 | P17;
            GP10 =0; GP12 =0; GP13 = 0; GP14 =0; GP15 = 0; GP16 =0; GP17 = 0; //反电动势端口需要输出为0，1会漏电
            SetBit(P1_PU, P11HV_EN);  // P11 高压输入模式，保持使能
            
            
            P4_OE = P43 | P44;
            P4_PU = P41 | P42 | P45 | P46;
            GP43 =0;  GP44 =0;
            /*- Demo板配置：悬空引脚配置为上拉，反电动势配置为输出0，输入检测引脚配置为输出0—-*/  
            #endif        
            SleepSet.SleepDelayCout = 0;
            SleepSet.SleepFlag = 1;
            
            SetBit(CCFG1, VBB_DIS);          // 关闭HVIC供电     ClrBit(CCFG1, VBB_DIS);  //使能输出后需要延时等待1ms以上
            SetBit(P1_IE, P11);               // config P11 as the source of EXTI1
            SetBit(PCON, STOP);

        }  
    }
}